import pandas
import numpy
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB,BernoulliNB,MultinomialNB # 这种分类器使用高斯分布为先验分布
df=pandas.read_csv('watermelon.csv',skipinitialspace=True,sep=',',header=0,index_col=0,na_filter=False)
dic_target = {'是': 1, '否': 0, }
dic_color = {'青绿': 1, '乌黑': 2, '浅白': 0}
dic_found = {'蜷缩': 1, '稍缩': 2, '硬挺': 0}
dic_knock = {'浊响': 2, '沉闷': 1, '清脆': 0}
dic_texture = {'清晰':0, '稍糊': 1, '模糊': 2}
dic_umbilical = {'凹陷': 0, '稍凹': 1, '平坦': 2}
dic_touch = {'硬滑': 1, '软粘': 0}
#     2.1.2 映射目标
df['纹理'] = df['纹理'].map(dic_texture)
df['好瓜'] = df['好瓜'].map(dic_target)
df['色泽'] = df['色泽'].map(dic_color)
df['根蒂'] = df['根蒂'].map(dic_found)
df['敲声'] = df['敲声'].map(dic_knock)
df['脐部'] = df['脐部'].map(dic_umbilical)
df['触感'] = df['触感'].map(dic_touch)

target=df['好瓜'].values
data=df.iloc[:,:-1].values
Xtrain, Xtest, Ytrain, Ytest = train_test_split(data,target, test_size=0.2)
classier=MultinomialNB()
classier.fit(Xtrain,Ytrain)
s=classier.score(Xtest,Ytest)
print(s)
